package com.jk.service.impl;

import com.jk.mapper.EnterpriseMapper;
import com.jk.service.EnterpriseService;
import com.jk.vo.CouponInfo;
import com.jk.vo.ProductInfo;
import com.jk.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业模块service层
 */
@Service("enterpriseService")
public class EnterpriseServiceImpl implements EnterpriseService {
    @Autowired
    private EnterpriseMapper enterpriseMapper;

    /**
     * 新增优惠券信息
     * @param couponInfo
     * @return
     */
    @Transactional(readOnly=false,propagation= Propagation.REQUIRED)
    public String insertCouponInfo(CouponInfo couponInfo) {
        int marker = enterpriseMapper.insertCouponInfo(couponInfo);
        String flag = "";
        if (marker==1){
            flag = "新增优惠券信息成功";
        }else{
            flag ="新增优惠券信息错误";
        }
        return flag;
    }

    /**
     * 新增产品信息
     * @param productInfo
     * @return
     */
    public String insertProductInfo(ProductInfo productInfo) {
        int mark = enterpriseMapper.insertProductInfo(productInfo);
        String flag = "";
        if (mark==1){
            flag = "新增产品信息成功Success";
        }else{
            flag ="新增产品信息错误Error";
        }
        return flag;
    }

    /**
     * 分页查询商品信息
     * @param rows
     * @param page
     * @return
     */
    public Map selectProductInfo(int rows, int page) {
        //分页查询开始未知，查询条数为rows
        int start = (page-1)*rows;
        int end = rows;
        //查询总条数
        int totalCount = enterpriseMapper.selectTotalCount();
        //查询分页信息、
        List<ProductInfo> proList = enterpriseMapper.selectProductInfo(start ,end);
        Map proMap = new HashMap<String,Object>();
        proMap.put("total",totalCount);
        proMap.put("rows",proList);
        return proMap;
    }

    /**
     * 查询企业用户信息
     * @param userId
     * @return
     */
    public UserInfo selectEnterpriseInfo(String userId) {

        return enterpriseMapper.selectEnterpriseInfo(userId);
    }
}
